package com.xiaoyu.izf.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.xiaoyu.izf.annotation.RequestAuth;
import com.xiaoyu.izf.model.MerchantInfo;
import com.xiaoyu.izf.model.Region;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.Vocation;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.service.MerchantInfoService;
import com.xiaoyu.izf.service.RegionService;
import com.xiaoyu.izf.service.UserInfoService;
import com.xiaoyu.izf.service.VocationService;
import com.xiaoyu.izf.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 商户
 */
@RestController
@RequestMapping("/merchant")
public class MerchantController extends BaseController{

    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private RegionService regionService;
    @Autowired
    private VocationService vocationService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 商户列表
     * @param request
     * @param body
     * @return
     */
    @RequestMapping("/list")
    @RequestAuth
    public DataTableBean merchantList(HttpServletRequest request, @RequestBody String body){
        UserInfo user = user(request);
//        UserInfo user = userInfoService.findByUserName("15208344647");
        ParamListBean paramListBean = getParamListBean(body);
        DataTableBean dataTableBean = merchantInfoService.parList(paramListBean,user);
        return dataTableBean;
    }

    /**
     * 商户添加
     * @param merchant 商户基本信息
     * @param vocationId1 商户类别id
     * @param vocationId2 一级行业id
     * @param vocationId3 二级行业id
     * @param regionId1 省份id
     * @param regionId2 城市id
     * @param regionId3 区域id
     * @param addressDetail 地址详情
     * @param idNumberTime 身份证有效期：2018-01-01至2019-10-30
     * @param licenceTime 营业执照有效期：2018-01-01至2019-10-30
     * @return
     */
    @RequestMapping("/add")
    @RequestAuth
    public FormResultBean add(MerchantInfo merchant,int vocationId1,int vocationId2,int vocationId3,
                              int regionId1,int regionId2,int regionId3,String addressDetail,
                              String idNumberTime,String licenceTime,HttpServletRequest request){
        UserInfo user = user(request);
        FormResultBean resultBean = new FormResultBean();

        if (StrUtil.isBlank(merchant.getMerchantName()))
            return resultBean.setType("error").setMessage("商户名称不能为空");
        if (StrUtil.isBlank(merchant.getMerchantSimpleName()))
            return resultBean.setType("error").setMessage("商户简称不能为空");
        if (StrUtil.isBlank(merchant.getLinkmanName()))
            return resultBean.setType("error").setMessage("联系人姓名不能为空");
        if (StrUtil.isBlank(merchant.getLinkmanMobile())){
            return resultBean.setType("error").setMessage("联系人电话不能为空");
        }else if (!Validator.isMobile(merchant.getLinkmanMobile())){
            return resultBean.setType("error").setMessage("联系人电话格式不正确");
        }
        if (StrUtil.isBlank(merchant.getServicePhone())){
            return resultBean.setType("error").setMessage("客服电话不能为空");
        }else if (!CommonUtil.isPhone(merchant.getServicePhone())){
            return resultBean.setType("error").setMessage("客服电话格式不正确");
        }
        if (StrUtil.isBlank(merchant.getEmail())){
            return resultBean.setType("error").setMessage("电子邮箱不能为空");
        }else if (!Validator.isEmail(merchant.getEmail())){
            return resultBean.setType("error").setMessage("电子邮箱格式不正确");
        }
        Vocation v1 = vocationService.findByVocationId(vocationId1);
        if (v1==null)
            return resultBean.setType("error").setMessage("商户类别不存在");
        Vocation v2 = vocationService.findByVocationId(vocationId2);
        if (v2==null)
            return resultBean.setType("error").setMessage("一级行业不存在");
        Vocation v3 = vocationService.findByVocationId(vocationId3);
        if (v3==null)
            return resultBean.setType("error").setMessage("二级行业不存在");
        Region r1 = regionService.findByRegionId(regionId1);
        if (r1==null)
            return resultBean.setType("error").setMessage("省份不存在");
        Region r2 = regionService.findByRegionId(regionId2);
        if (r2==null)
            return resultBean.setType("error").setMessage("城市不存在");
        Region r3 = regionService.findByRegionId(regionId3);
        if (r3==null)
            return resultBean.setType("error").setMessage("区/县不存在");
        if (StrUtil.isBlank(addressDetail))
            return resultBean.setType("error").setMessage("详细地址不能为空");
        if (StrUtil.isBlank(merchant.getLegalPersonName()))
            return resultBean.setType("error").setMessage("法人姓名不能为空");
        if (StrUtil.isBlank(merchant.getLegalPersonMobile())){
            return resultBean.setType("error").setMessage("法人电话不能为空");
        }else if (!Validator.isMobile(merchant.getLegalPersonMobile())){
            return resultBean.setType("error").setMessage("法人电话格式不正确");
        }
        if (StrUtil.isBlank(merchant.getLegalPersonIdNumber())){
            return resultBean.setType("error").setMessage("法人身份证号码不能为空");
        }else if (!Validator.isCitizenId(merchant.getLegalPersonIdNumber())){
            return resultBean.setType("error").setMessage("法人身份证号码格式不正确");
        }
        if (StrUtil.isBlank(merchant.getBusinessLicenseNumber()))
            return resultBean.setType("error").setMessage("营业执照号不能为空");

        merchant.setMerchantTypeInfo(v1.getVocationName()+"--"+v2.getVocationName()+"--"+v3.getVocationName());
        merchant.setMerchantAddress(r1.getRegionName()+"--"+r2.getRegionName()+"--"+r3.getRegionName()+"--"+addressDetail);
        merchant.setRegionCode(r3.getRegionCode());

        String[] idnumberTimes = idNumberTime.split("至");
        String[] licencenumberTimes = licenceTime.split("至");
        merchant.setLegalPersonIdNumberStart(DateUtil.parseDate(idnumberTimes[0]).toSqlDate());
        merchant.setLegalPersonIdNumberEnd(DateUtil.parseDate(idnumberTimes[1]).toSqlDate());
        merchant.setBusinessLicenseStart(DateUtil.parseDate(licencenumberTimes[0]).toSqlDate());
        merchant.setBusinessLicenseEnd(DateUtil.parseDate(licencenumberTimes[1]).toSqlDate());

        merchantInfoService.add(merchant,user.getId());

        return resultBean;
    }
}
